<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>交通流量时段分析</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-card {border:1px solid #f2f2f2;border-radius:5px;}
        .icon {margin-right:10px;color:#1aa094;}
        .icon-cray {color:#ffb800!important;}
        .icon-blue {color:#1e9fff!important;}
        .icon-tip {color:#ff5722!important;}
        .layuimini-qiuck-module {text-align:center;margin-top: 10px}
        .layuimini-qiuck-module a i {display:inline-block;width:100%;height:60px;line-height:60px;text-align:center;border-radius:2px;font-size:30px;background-color:#F8F8F8;color:#333;transition:all .3s;-webkit-transition:all .3s;}
        .layuimini-qiuck-module a cite {position:relative;top:2px;display:block;color:#666;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;font-size:14px;}
        .welcome-module {width:100%;height:210px;}
        .panel {background-color:#fff;border:1px solid transparent;border-radius:3px;-webkit-box-shadow:0 1px 1px rgba(0,0,0,.05);box-shadow:0 1px 1px rgba(0,0,0,.05)}
        .panel-body {padding:10px}
        .panel-title {margin-top:0;margin-bottom:0;font-size:12px;color:inherit}
        .label {display:inline;padding:.2em .6em .3em;font-size:75%;font-weight:700;line-height:1;color:#fff;text-align:center;white-space:nowrap;vertical-align:baseline;border-radius:.25em;margin-top: .3em;}
        .layui-red {color:red}
        .main_btn > p {height:40px;}
        .layui-bg-number {background-color:#F8F8F8;}
        .layuimini-notice:hover {background:#f6f6f6;}
        .layuimini-notice {padding:7px 16px;clear:both;font-size:12px !important;cursor:pointer;position:relative;transition:background 0.2s ease-in-out;}
        .layuimini-notice-title,.layuimini-notice-label {
            padding-right: 70px !important;text-overflow:ellipsis!important;overflow:hidden!important;white-space:nowrap!important;}
        .layuimini-notice-title {line-height:28px;font-size:14px;}
        .layuimini-notice-extra {position:absolute;top:50%;margin-top:-8px;right:16px;display:inline-block;height:16px;color:#999;}
    
        .cross-pos .layui-input-inline {width:350px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md6">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md12">
                        <div class="layui-card">
                            <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>24小时内时段车流量统计</div>
                            <div class="layui-card-body">
                                <fieldset class="table-search-fieldset">
                                    <legend>路口选取</legend>
                                    <div style="margin: 10px 10px 10px 10px">
                                        <form class="layui-form layui-form-pane" action="">
                                            <div class="layui-form-item cross-pos">
                                                <!-- <div class="layui-inline"> -->
                                                    <label class="layui-form-label">路口位置</label>
                                                    <div class="layui-input-inline" lay-key="7">
                                                        <input type="text" name="" placeholder="默认：全部" autocomplete="off" class="layui-input" id="cur-cross">
                                                    </div>
                                                <!-- </div> -->
                                            </div>
                                        </form>
                                    </div>
                                </fieldset>
                                <div id="time-car-flow-line" style="width: 100%;min-height:500px"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md12">
                        <div class="layui-card">
                            <div class="layui-card-header"><i class="fa fa-dot-circle-o icon"></i>本月交通车流量分布</div>
                            <div class="layui-card-body">
                                <iframe src="heaps/heatmap4.html" id="time-car-flow-heat" style="width: 100%;height:600px">
                                </iframe>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header"><i class="fa fa-bar-chart icon"></i>实时车流量分布</div>
                    <div class="layui-card-body">
                        <fieldset class="table-search-fieldset">
                            <legend>路口选取</legend>
                            <div style="margin: 10px 10px 10px 10px">
                                <form class="layui-form layui-form-pane" action="">
                                    <div class="layui-form-item cross-pos">
                                        <!-- <div class="layui-inline"> -->
                                            <label class="layui-form-label">路口位置</label>
                                            <div class="layui-input-inline" lay-key="7">
                                                <input type="text" name="" placeholder="默认：全部" autocomplete="off" class="layui-input" id="cur-cross">
                                            </div>
                                        <!-- </div> -->
                                    </div>
                                </form>
                            </div>
                        </fieldset>
                        <div id="chart-panel" style="width: 100%;height:600px; padding-top:20px;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['layer', 'miniTab', 'echarts', 'tableSelect'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            miniTab = layui.miniTab,
            echarts = layui.echarts,
            tableSelect = layui.tableSelect;

        miniTab.listen();

        var echartsTimeFlow = echarts.init(document.getElementById('time-car-flow-line'), 'walden');
        var optionTimeFlow = {
            tooltip: {
                trigger: 'axis'
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: []
            },
            yAxis: {
                type: 'value'
            },
            series: [
                {
                    name:'',
                    type:'line',
                    symbolSize:3,
                    itemStyle: {
                        normal: {
                            label: {
                                show: true,
                                position: 'top',
                                fontSize: 9,
                                textStyle: {color: 'black'}
                            }
                        }
                    },
                    data:[]
                }
            ]
        };
        (function initTimeFlow(charts, flowOpt) {
            let timeUrl = '../api/carFlow.json';
            $.getJSON(timeUrl, function (response) {
                console.log(response);
                let flowData = response.data;
                for (let i=0, len=flowData.length; i < len; i++) {
                    flowOpt.xAxis.data.push(flowData[i][0]);
                    flowOpt.series[0].data.push(flowData[i][1]);
                }
                charts.setOption(optionTimeFlow);
                // echarts 窗口缩放自适应
                window.onresize = function(){
                    charts.resize();
                }
            });
        })(echartsTimeFlow, optionTimeFlow);

        function getNum(minNum, maxNum){
            switch(arguments.length){ 
                case 1: 
                    return parseInt(Math.random()*minNum+1,10); 
                    break; 
                case 2: 
                    return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); 
                    break; 
                default: return 0; break; 
            } 
        }

        tableSelect.render({
            elem: '#cur-cross',	// 定义输入框input对象 必填
            checkedKey: 'id', // 表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'keyword',	// 搜索输入框的name值 默认keyword
            searchPlaceholder: '关键词搜索',	// 搜索输入框的提示文字 默认关键词搜索
            height: '350',  // 自定义高度
            width: '350',  // 自定义宽度
            table: {	// 定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '../api/cross.json',
                cols: [[
                    { type: 'radio' },
                    { field: 'id', title: '编号' },
                    { field: 'addr', title: '地址' }
                ]]
            },
            done: function (elem, data) {
                // 选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                // 拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var NEWJSON = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.addr);
                })
                elem.val(NEWJSON.join(","));

                (function (charts, opt){
                    let data = opt.series[0].data;
                    for (let i=0, len=data.length; i<len; i++) {
                        let randId = getNum(3330, 25000);
                        if (randId % 2 == 1) {
                            while (randId > data[i]) randId = Math.floor(randId / 2);
                            data[i] = data[i] - randId;
                        } else {
                            data[i] = data[i] + randId;
                        }
                    }
                    charts.setOption(opt);
                })(echartsTimeFlow, optionTimeFlow);
            }
        });

        
        var app = {};
        var optInTime;
        const categories = (function () {
            let now = new Date();
            let res = [];
            let len = 10;
            while (len--) {
                res.unshift(now.toLocaleTimeString().replace(/^\D*/, ''));
                now = new Date(+now - 2000);
            }
            return res;
        })();
        const categories2 = (function () {
            let res = [];
            let len = 10;
            while (len--) {
                res.push(10 - len - 1);
            }
            return res;
        })();
        const data = (function () {
            let res = [];
            let len = 10;
            while (len--) {
                res.push(Math.round(Math.random() * 1000));
            }
            return res;
        })();
        const data2 = (function () {
            let res = [];
            let len = 0;
            while (len < 10) {
                res.push(+(Math.random() * 10 + 5).toFixed(1));
                len++;
            }
            return res;
        })();
        optInTime = {
            title: {
                text: '实时交通流量分析'
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#283b56'
                    }
                }
            },
            grid: {
                left: '5%',
                top: '20%',
                right: '5%'
            },
            legend: {},
            toolbox: {
                show: true,
                feature: {
                    dataView: { readOnly: false },
                    restore: {},
                    saveAsImage: {}
                }
            },
            dataZoom: {
                show: false,
                start: 0,
                end: 100
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: true,
                    data: categories
                },
                {
                    type: 'category',
                    boundaryGap: true,
                    data: categories2
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    scale: true,
                    name: '违法违章发生机率',
                    max: 30,
                    min: 0,
                    boundaryGap: [0.2, 0.2]
                },
                {
                    type: 'value',
                    scale: true,
                    name: '交通流量',
                    max: 1200,
                    min: 0,
                    boundaryGap: [0.2, 0.2]
                }
            ],
            series: [
                {
                    name: '交通流量',
                    type: 'bar',
                    xAxisIndex: 1,
                    yAxisIndex: 1,
                    data: data
                },
                {
                    name: '违法违章发生机率',
                    type: 'line',
                    data: data2
                }
            ]
        };
        app.count = 11;
        setInterval(function () {
            let axisData = new Date().toLocaleTimeString().replace(/^\D*/, '');
            data.shift();
            data.push(Math.round(Math.random() * 1000));
            data2.shift();
            data2.push(+(Math.random() * 10 + 5).toFixed(1));
            categories.shift();
            categories.push(axisData);
            categories2.shift();
            categories2.push(app.count++);
            echartsInTime.setOption({
                xAxis: [
                    { data: categories },
                    { data: categories2 }
                ],
                series: [
                    { data: data },
                    { data: data2 }
                ]
            });
        }, 2100);
        var echartsInTime = echarts.init(document.getElementById('chart-panel'), 'walden');
        echartsInTime.setOption(optInTime);
    });
</script>
</body>
</html>
